﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll;
using CSP.Web.Master;
using CSP.Web.UserControls;
using CSP.Bll.IObjectBlls;
using CSP.Common.Web;
using CSP.Model.CSPEnum;

namespace CSP.Web.MyHome.Activity.CollectBusinessInfo
{
    public partial class List : CSPMyHome
    {

        private List<int> _searchIds;
        private List<int> SearchIds
        {
            get
            {
                return ViewState[SEARCHIDSKEY] as List<int>;

            }
            set
            {
                ViewState[SEARCHIDSKEY] = _searchIds = value;
            }
        }

        private const string SEARCHIDSKEY = "searchids";

        protected void Page_Load(object sender, EventArgs e)
        {
            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.ServiceInfo;

            if (!IsPostBack)
            {
                if (Request["deleteid"] != null)
                {

                    string result = RemoveCollectBusinessInfo(Convert.ToInt32(Request["deleteid"]));
                    Response.ClearContent();
                    Response.Write(result);
                    Response.End();
                }
                else
                {
                    BindDefaultUIByUserName();
                }

            }
        }

        /// <summary>
        /// 通过用户名绑定默认UI
        /// </summary>
        private void BindDefaultUIByUserName()
        {
            SearchIds = GetIdsByUser();

            InitParm();
            this.S_Status.SelectedIndex = 0;
            ICollection<CSP.Model.CSP.CollectBusinessInfo> collectBusinessInfoes = FindByIds(SearchIds, U_CollectBusinessInfoPager.PageIndex, U_CollectBusinessInfoPager.PageSize);
            BindServiceInfoListUI(collectBusinessInfoes);
        }

        /// <summary>
        /// 绑定ServiceInfo到List上
        /// </summary>
        private void BindServiceInfoListUI(ICollection<CSP.Model.CSP.CollectBusinessInfo> collectBusinessInfoes)
        {
            this.R_CollectBusinessInfo.DataSource = collectBusinessInfoes;
            this.R_CollectBusinessInfo.DataBind();
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        private void InitParm()
        {
            SetPagerParm(0, 15, SearchIds.Count);
        }

        /// <summary>
        /// 设置分页参数
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        private void SetPagerParm(int pageIndex, int pageSize, int pageCount)
        {
            this.U_CollectBusinessInfoPager.PageSize = pageSize;
            this.U_CollectBusinessInfoPager.PageIndex = pageIndex;
            this.U_CollectBusinessInfoPager.ItemCount = pageCount;
        }

        /// <summary>
        /// 根据用户信息获取Ids
        /// </summary>
        private List<int> GetIdsByUser()
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            List<int> result;
            using (ICollectBusinessInfoBll collectBusinessBll = bllFactory.GetCollectBusinessInfoBll())
            {
                result = collectBusinessBll.FindIdsByUserName(UserProvider.UserName);
            }
            if (result == null)
            { 
                result = new List<int>();
            }
            return result;
        }

        /// <summary>
        /// 根据用户信息获取Ids
        /// </summary>
        /// <returns></returns>
        private List<int> FindIdsByUser()
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            List<int> ids = new List<int>();
            using (IServiceInfoBll serviceInfoBll = bllFactory.GetServiceInfoBll())
            {
                ids = serviceInfoBll.FindIdsByUser(UserProvider.UserName);
            }
            return ids;
        }

        /// <summary>
        /// 根据SearchValue获取结果ID
        /// </summary>
        /// <returns></returns>
        private List<int> FindIdsByName()
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            List<int> ids = new List<int>();
            using (ICollectBusinessInfoBll serviceInfoBll = bllFactory.GetCollectBusinessInfoBll())
            {
                ids = serviceInfoBll.FindIdsByNameAndUserName(Txt_SearchValue.Value.Trim(),UserProvider.UserName);
            }
            return ids;
        }

        /// <summary>
        /// status 获取结果Id
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private List<int> FindIdsByStatus( CollectBusinessInfoStatus status)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            List<int> serviceIds = new List<int>();
            using (ICollectBusinessInfoBll collectBusinessInfoBll = bllFactory.GetCollectBusinessInfoBll())
            {
                serviceIds = collectBusinessInfoBll.FindIdsByStatusAndUserName(status,UserProvider.UserName);
            }
            return serviceIds;
        }

        /// <summary>
        /// 根据Ids 获取数据
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        private ICollection<CSP.Model.CSP.CollectBusinessInfo> FindByIds(List<int> ids, int pageIndex, int pageSize)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            ICollection<CSP.Model.CSP.CollectBusinessInfo> collectBusinessInfoes = new List<CSP.Model.CSP.CollectBusinessInfo>();
            using (ICollectBusinessInfoBll collectBusinessInfoBll = bllFactory.GetCollectBusinessInfoBll())
            {
                collectBusinessInfoes = collectBusinessInfoBll.FindByIdsAndPageIndexAndPageSize(ids, pageIndex, pageSize);
            }
            return collectBusinessInfoes;
        }

        protected void U_CollectBusinessInfo_IndexChange(object sender, EventArgs e)
        {
            ICollection<CSP.Model.CSP.CollectBusinessInfo> collectBusinessInfoes = FindByIds(SearchIds, U_CollectBusinessInfoPager.PageIndex, U_CollectBusinessInfoPager.PageSize);
            BindServiceInfoListUI(collectBusinessInfoes);
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {


            if (Txt_SearchValue.Value.Trim() == "")
            {
                SearchIds = GetIdsByUser();
            }
            else
            {
                SearchIds = FindIdsByName();
            }
            InitParm();
            ICollection<CSP.Model.CSP.CollectBusinessInfo> collectBusinessInfoes = FindByIds(SearchIds, U_CollectBusinessInfoPager.PageIndex, U_CollectBusinessInfoPager.PageSize);

            BindServiceInfoListUI(collectBusinessInfoes);
        }

        protected void S_Status_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitParm();
            List<int> ids = new List<int>();
            if (S_Status.SelectedValue != "")
            {
                ids = FindIdsByStatus((CollectBusinessInfoStatus)Convert.ToInt32(S_Status.SelectedValue));
            }
            else
            {
                ids = SearchIds;
            }

            ICollection<CSP.Model.CSP.CollectBusinessInfo> serviceInfoes = FindByIds(ids, U_CollectBusinessInfoPager.PageIndex, U_CollectBusinessInfoPager.PageSize);

            BindServiceInfoListUI(serviceInfoes);
        }



        /// <summary>
        /// 获取操作名称
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string GetNameByStatus(string status)
        {
            string result = "审核中";
            //如果当前是未上线和上线状态那么,操作按钮名称为上线
            if ((CollectBusinessInfoStatus)Convert.ToInt32(status) == CollectBusinessInfoStatus.Processing)
            {
                result = "审核中";
            }
            else if ((CollectBusinessInfoStatus)Convert.ToInt32(status) == CollectBusinessInfoStatus.Accomplish)//如果当前是未上线和上线状态那么,操作按钮名称为上线
            {
                result = "审核通过";
            }
            else
            {
                result = "审核失败";
            }

            return result;
        }

        protected string GetLastSecond(string date)
        {
            if (date == "")
            {
                return "0";
            }
            DateTime dt = Convert.ToDateTime(date);
            dt = dt.AddDays(7);
            return (dt - DateTime.Now).TotalSeconds.ToString();
        }

        

        /// <summary>
        /// 删除服务信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private string RemoveCollectBusinessInfo(int id)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            CSP.Model.CSP.CollectBusinessInfo collectBusinessInfo = null;
            try
            {
                using (ICollectBusinessInfoBll collectBusinessInfovBll = bllFactory.GetCollectBusinessInfoBll())
                {
                    collectBusinessInfo = collectBusinessInfovBll.DeleteByIdAndUser(id, UserProvider.UserName);
                    if (collectBusinessInfo == null)
                    {
                        return "{\"status\":2,\"msg\":\"权限不足!\"}";
                    }
                    return "{\"status\":1,\"msg\":\"操作成功!\"}";
                }
            }
            catch (Exception ex)
            {
                LoggerProvider.Error(id, ex);
                return "{\"status\":0,\"msg\":\"操作失败!\"}";
            }

        }

        protected string IsShowOperate(string userName, int status)
        {
            if (userName == UserProvider.UserName && ((CollectBusinessInfoStatus)status == CollectBusinessInfoStatus.Processing || (CollectBusinessInfoStatus)status == CollectBusinessInfoStatus.Failed))
            {
                return "block";
            }
            else 
            {
                return "none";
            }
        }
    }
}